<?php

namespace App\Api\Controllers;

use App\Api\Services\StorageManageService;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class StorageManageController extends BaseController
{
    /**
     * 库存列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/19 16:06
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(
            StorageManageService::list($this->getMainUserId(), $data)
        );
    }

    /**
     * 批次列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/7 16:20
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//页面大小
        ],[
            'id.required' => '参数`库存主键id`缺失',
            'id.integer' => '参数`库存主键id`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }


        $result = StorageManageService::batchList($request->id);

        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $result]);
    }

    /**
     * 批次采购详细
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/7 16:23
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchMaterielInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//页面大小
        ],[
            'id.required' => '参数`批次主键id`缺失',
            'id.integer' => '参数`批次主键id`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $result = StorageManageService::batchMaterielInfo($request->id);
        return $this->success($result);
    }

    public function readBatchNo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'batch_no' =>'required',//条码信息
        ],[
            'batch_no.required' => '参数`条码信息`缺失',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return response()->json(
            StorageManageService::readBatchNo($data)
        );
    }

    /**
     * 商品库存批次列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/6/24 15:55
     * @return \Illuminate\Http\JsonResponse
     */
    public function materielBatchList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'materiel_type_id' =>'required',//页面大小
            'sell_id' => 'required',
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'type' => [
                'required',
                Rule::In([0,1]),
            ],//条码类型0:商品;1:套餐;
        ],[
            'materiel_type_id.required' => '参数`商品id`缺失',
            'sell_id.required' => '参数`销售单id`缺失',
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(
            StorageManageService::materielBatchList(
                $request->materiel_type_id,
                $request->sell_id,
                $request->type,
                $data
            )
        );
    }

    public function  fastOutStorage(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'batch_no' =>'required',//页面大小
//            'sell_id' => 'required',
        ],[
            'batch_no.required' => '参数`批次编号`缺失',
//            'sell_id.required' => '参数`销售单id`缺失',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if($this->getOutWay()) {
            return $this->fail('', '您已开批次号，无法快速出库');
        }

        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $data['out_way'] = $this->getOutWay();

        $this->log('商品或套餐快速出库', $data);

        return response()->json(
            StorageManageService::fastOutStorage($data)
        );
    }

    /**
     * 清除批次信息
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/7/13 16:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function clear(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//页面大小
        ],[
            'id.required' => '参数`库存主键id`缺失',
            'id.integer' => '参数`库存主键id`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $this->log('清除库存', $data);

        return response()->json(
            StorageManageService::clear($data)
        );
    }
}
